﻿const Storage  = {
    // 获取本地存储中的值
    get(key, flag) {
        try {
            var value;
            if (flag) {
                value = sessionStorage.getItem(key);
                if (value === null || value === undefined || value === "") {
                    return null;
                } 
                return JSON.parse(sessionStorage.getItem(key));
            } else {
                value = localStorage.getItem(key);
                if (value === null || value === undefined || value === "") {
                    return null;
                }
                return JSON.parse(localStorage.getItem(key));
            }




        } catch (err) {
            return null;
        }
    },

    // 设置本地存储中的值
    set(key, value, flag) {



        if (flag == true) {
            return sessionStorage.setItem(key, JSON.stringify(value));
        } else {
            return localStorage.setItem(key, JSON.stringify(value));
        }
    },

    // 删除本地存储中的值
    remove(key, flag) {

        if (flag == true) {
            return sessionStorage.removeItem(key);
        } else {
            return localStorage.removeItem(key);
        }


    },

    // 清空本地存储
    clear(flag) {
        if (flag == true) {
            return sessionStorage.clear();
        } else {
            return localStorage.clear();
        }
   
    },

    // 获取 localsotrage 中以某个字符开头的所有数据
    // const data = LocalStorageManager.getDataStartingWith(LocalStorageManager.recordKey);
    getDataStartingWith(prefix) {
        let data = {};
        for (let key in localStorage) {
            if (key.startsWith(prefix)) {
                data[key] = JSON.parse(localStorage.getItem(key));
            }
        }
        return data;
    }
};
export default Storage